<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.title" clearable class="filter-item" style="width: 200px;" placeholder="请输入活动名称" />
      <el-button v-permission="['GET /admin/activity/list']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button v-permission="['POST /admin/activity/create']" class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate">添加</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="活动标题" prop="title" />
      <el-table-column align="center" label="活动类型">
        <template slot-scope="scope">{{ scope.row.type | formatType }}</template>
      </el-table-column>
      <el-table-column align="center" label="开始时间" prop="startTime" />
      <el-table-column align="center" label="结束时间" prop="endTime" />
      <el-table-column prop="serviceIntroduce" label="活动数据" width="280" align="center">
        <!-- 分行显示 -->
        <template slot-scope="scope">
          <div>成团数：{{ scope.row.groupons }} </div>
          <div>报名数：{{ scope.row.applications }} </div>
          <div>浏览数：{{ scope.row.views }} </div>
          <div>分享数：{{ scope.row.shares }}  </div>
        </template>
      </el-table-column>
      <el-table-column align="center" property="qcode" label="活动二维码">
        <template v-if="src != ''" slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="'data:image/png;base64,' + scope.row.qcode" :preview-src-list="['data:image/png;base64,' + scope.row.qcode]" onerror="" :lazy="true" class="el-icon-circle-close" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="活动状态" prop="state">
        <template slot-scope="scope">{{ scope.row.state | formatState }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- 活动编辑 -->
          <el-button v-if="scope.row.state === 0 | scope.row.state === 3" v-permission="['POST /admin/activity/changeState']" type="primary" size="mini" @click="handleIssueState(scope.row)">立刻发布</el-button>
          <el-button v-if="scope.row.state === 1" v-permission="['POST /admin/activity/changeState']" type="primary" size="mini" @click="handleDownState(scope.row)">暂停活动</el-button>
          <el-button v-if="scope.row.state !== 2" v-permission="['POST /admin/activity/edit']" type="danger" size="mini" @click="handleUpdate(scope.row)">编辑活动</el-button>
          <!-- <el-button v-if="scope.row.state === 2 | scope.row.state === 3"  type="danger" size="mini" @click="handleUpdate(scope.row)">活动详情</el-button> -->
          <!-- 活动详情 -->
          <el-button v-permission="['POST /admin/activityGroupon/list']" type="primary" size="mini" @click="activityDetail(scope.row)">活动详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<style>
  .table-expand {
    font-size: 0;
  }

  .table-expand label {
    width: 100px;
    color: #99a9bf;
  }

  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }

  .gallery {
    width: 80px;
    margin-right: 10px;
  }

  .el-icon-circle-close {
      color: white;
  }

</style>

<script>
import { activitylist, changeActivityState } from '@/api/activitylist'
import Pagination from '@/components/Pagination'

const defaultTypeOptions = [
  {
    label: '团购',
    value: 1
  }
]

export default {
  name: 'Activitylist',
  components: { Pagination },
  filters: {
    formatType(type) {
      for (let i = 0; i < defaultTypeOptions.length; i++) {
        if (type === defaultTypeOptions[i].value) {
          return defaultTypeOptions[i].label
        }
      }
      return ''
    },
    formatState(state) {
      if (state === 0) {
        return '待发布'
      } else if (state === 1) {
        return '发布中'
      } else {
        return '下线'
      }
    }
  },
  data() {
    return {
      typeOptions: Object.assign({}, defaultTypeOptions),
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        sort: 'add_time',
        order: 'desc'
      },
      detailDialogVisible: false,
      downloadLoading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        name: [
          { required: true, message: '活动标题不能为空', trigger: 'blur' }
        ]
      },
      dataForm: {
        id: undefined,
        total: 0,
        limit: 1,
        type: 0,
        status: 0,
        startTime: null,
        endTime: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      activitylist(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDownload() {
    },
    activityDetail(row) {
      this.$router.push({ path: '/promotion/activity-detail', query: { id: row.id }})
    },
    handleUpdate(row) {
      this.$router.push({ path: '/promotion/activity-edit', query: { id: row.id }})
    },
    handleIssueState(row) {
      changeActivityState({ id: row.id, state: 1 }).then(response => {
        this.$notify.success({
          title: '成功',
          message: '发布成功'
        })
        this.getList()
      }).catch(response => {
        this.$notify.error({
          title: '失败',
          message: response.data.errmsg
        })
      })
    },
    handleDownState(row) {
      changeActivityState({ id: row.id, state: 3 }).then(response => {
        this.$notify.success({
          title: '成功',
          message: '活动已下线'
        })
        this.getList()
      }).catch(response => {
        this.$notify.error({
          title: '失败',
          message: response.data.errmsg
        })
      })
    },
    handleCreate() {
      this.$router.push({ path: '/promotion/activity-create' })
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        total: 0,
        limit: 1,
        type: 0,
        status: 0,
        startTime: null,
        endTime: null
      }
    }
  }
}
</script>
